package com.vtest.quartz.service.impl.needle;

import com.vtest.quartz.domain.TDEntity;
import com.vtest.quartz.domain.needle.*;
import com.vtest.quartz.mapper.needle.NeedleMapper;
import com.vtest.quartz.mapper.vtest.report.CommonReportMapper;
import com.vtest.quartz.service.needle.NeedleService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.awt.windows.WWindowPeer;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Field;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.WeekFields;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @program: Quartz
 * @author: tiger
 * @create: 2025-01-08 10:44
 * @Description : 针卡报表 service层
 */

@Service
public class NeedleServiceImpl implements NeedleService {

    @Autowired
    private NeedleMapper needleMapper;

    @Autowired
    private CommonReportMapper commonMapper;

    @Override
    public List<YOSNeedleEntity> findYOSNeedleInfo(String time) {
        List<YOSNeedleEntity> yosList = needleMapper.findYOSNeedleInfoList();

        List<String> idList = yosList.stream().map(entity -> entity.getProberCardId()).collect(Collectors.toList());


        List<String> workRecentWeekList = needleMapper.isWorkRecentWeekByIDAndTime(idList, time);

        //近一周是否工作
        for (YOSNeedleEntity yosEntity : yosList) {
            String proberCardId = yosEntity.getProberCardId();
            for (String id : workRecentWeekList) {
                if (proberCardId.equals(id)) {
                    yosEntity.setWorkFlag("是");
                    break;
                }
            }
        }


        //获取最近一周有工作的ID
        List<String> workIDAndNullList = yosList.stream().map(yosNeedleEntity -> {
                    return yosNeedleEntity.getWorkFlag().equals("是") ? yosNeedleEntity.getProberCardId() : "";
                }
        ).collect(Collectors.toList());

        List<String> workIDList = workIDAndNullList.stream().filter(id -> id.length() != 0).collect(Collectors.toList());

        //查询片数

        LocalDate currentDate = LocalDate.now();
        // 使用WeekFields来定义一周的开始日
        WeekFields weekFields = WeekFields.of(Locale.getDefault());
        // 获取当前日期是一年中的第几周
        int weekNumber = currentDate.get(weekFields.weekOfYear());

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate lo_time = LocalDate.parse(time, dateTimeFormatter);

        for (int i = weekNumber; i > 0; i--) {

            LocalDate subDate = lo_time.minusWeeks(weekNumber - i - 1);
            LocalDate mondayDate = subDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            String mondy = mondayDate.format(dateTimeFormatter);
            System.out.println("周日:" + mondy + "  week" + weekNumber);
            //获取某一周的PCS
            List<Map<String, String>> mesTestPCSRecentBYIDList = needleMapper.findMESTestPCSRecentBYID(idList, mondy);//time 某一周的最后一天
            //给对象设置
            for (YOSNeedleEntity yosNeedleEntity : yosList) {
                for (Map<String, String> map : mesTestPCSRecentBYIDList) {
                    String id = map.get("ProberCardID");

                    if (yosNeedleEntity.getProberCardId().equals(id)) {
                        Class<? extends YOSWW> aClass = null;
                        if (id.equals("ASR1602-PC-01")) {
                            String s = "";
                        }
                        List<YOSWW> pcsList = yosNeedleEntity.getPcs();
                        YOSWW yosww = null;
                        if (pcsList == null) {
                            yosww = new YOSWW();
                            aClass = yosww.getClass();
                            ArrayList<YOSWW> yoList = new ArrayList<>();
                            yoList.add(yosww);
                            yosNeedleEntity.setPcs(yoList);
                        } else {
                            yosww = pcsList.get(0);
                            aClass = yosww.getClass();
                        }


                        try {
                            Field declaredField = aClass.getDeclaredField("w" + i);
                            declaredField.setAccessible(true);

                            Integer pcs = Integer.parseInt(map.get("PCS") + "");
                            declaredField.set(yosww, pcs);
                        } catch (NoSuchFieldException e) {
                            throw new RuntimeException(e);
                        } catch (IllegalAccessException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }


            }


        }

        List<Map<String, String>> mesTestPCSList = needleMapper.findMESTestPCSRecentBYID(workIDList, time);

        //最近一周测试片数
        for (YOSNeedleEntity yosEntity : yosList) {
            String proberCardId = yosEntity.getProberCardId();
            if (!yosEntity.getWorkFlag().equals("是")) continue;
            for (Map<String, String> map : mesTestPCSList) {
                String id = map.get("ProberCardID");
                if (proberCardId.equals(id)) {
                    Object str_pcs = map.get("PCS");
                    //yosEntity.setPcs(Integer.parseInt(str_pcs.toString()));
                }
            }
        }

        //初始针长
        List<Map<String, String>> inComingLenList = needleMapper.getInComingLenByID(idList);
        for (YOSNeedleEntity yosEntity : yosList) {
            String proberCardId = yosEntity.getProberCardId();
            for (Map<String, String> map : inComingLenList) {
                String id = map.get("prober_card_id");
                if (proberCardId.equals(id)) {
                    String inComingLen = map.get("pin_maxlen");
                    yosEntity.setPinMinlen(Double.parseDouble(inComingLen));
                    break;
                }
            }
        }

        //磨耗比
        for (YOSNeedleEntity yosEntity : yosList) {

            Integer totalTd = yosEntity.getTotalTd();
            Double pinMinlen = yosEntity.getPinMinlen();
            Double afterPinlen = yosEntity.getAfterPinlen();

            if (pinMinlen <= afterPinlen) {
                yosEntity.setMohao(0);
                continue;
            }
            double mohao = totalTd / (pinMinlen - afterPinlen);
            yosEntity.setMohao((int) Math.round(mohao));
        }

        return yosList;
    }


    @Override
    public List<BIDNeedleEntity> findBIDNeedleInfo(String time) {
        List<BIDNeedleEntity> bidList = needleMapper.findBIDNeedleInfoList();

        List<String> idList = bidList.stream().map(entity -> entity.getProberCardId()).collect(Collectors.toList());


        List<String> workRecentWeekList = needleMapper.isWorkRecentWeekByIDAndTime(idList, time);

        //近一周是否工作
        for (BIDNeedleEntity bidEntity : bidList) {
            String proberCardId = bidEntity.getProberCardId();
            for (String id : workRecentWeekList) {
                if (proberCardId.equals(id)) {
                    bidEntity.setWorkFlag("是");
                    break;
                }
            }
        }


        //获取最近一周有工作的ID
        List<String> workIDAndNullList = bidList.stream().map(bidNeedleEntity -> {
                    return bidNeedleEntity.getWorkFlag().equals("是") ? bidNeedleEntity.getProberCardId() : "";
                }
        ).collect(Collectors.toList());

        List<String> workIDList = workIDAndNullList.stream().filter(id -> id.length() != 0).collect(Collectors.toList());

        //查询片数

        LocalDate currentDate = LocalDate.now();
        // 使用WeekFields来定义一周的开始日
        WeekFields weekFields = WeekFields.of(Locale.getDefault());
        // 获取当前日期是一年中的第几周
        int weekNumber = currentDate.get(weekFields.weekOfYear());

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate lo_time = LocalDate.parse(time, dateTimeFormatter);

        for (int i = weekNumber; i > 0; i--) {

            LocalDate subDate = lo_time.minusWeeks(weekNumber - i - 1);
            LocalDate mondayDate = subDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            String mondy = mondayDate.format(dateTimeFormatter);
            System.out.println("周日:" + mondy + "  week" + weekNumber);
            //获取某一周的PCS
            List<Map<String, String>> mesTestPCSRecentBYIDList = needleMapper.findMESTestPCSRecentBYID(idList, mondy);//time 某一周的最后一天
            //给对象设置
            for (BIDNeedleEntity bidNeedleEntity : bidList) {
                for (Map<String, String> map : mesTestPCSRecentBYIDList) {
                    String id = map.get("ProberCardID");

                    if (bidNeedleEntity.getProberCardId().equals(id)) {
                        Class<? extends BIDWW> aClass = null;
                        if (id.equals("ASR1602-PC-01")) {
                            String s = "";
                        }
                        List<BIDWW> pcsList = bidNeedleEntity.getPcs();
                        BIDWW bidww = null;
                        if (pcsList == null) {
                            bidww = new BIDWW();
                            aClass = bidww.getClass();
                            ArrayList<BIDWW> biList = new ArrayList<>();
                            biList.add(bidww);
                            bidNeedleEntity.setPcs(biList);
                        } else {
                            bidww = pcsList.get(0);
                            aClass = bidww.getClass();
                        }


                        try {
                            Field declaredField = aClass.getDeclaredField("w" + i);
                            declaredField.setAccessible(true);

                            Integer pcs = Integer.parseInt(map.get("PCS") + "");
                            declaredField.set(bidww, pcs);
                        } catch (NoSuchFieldException e) {
                            throw new RuntimeException(e);
                        } catch (IllegalAccessException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }


            }


        }

        List<Map<String, String>> mesTestPCSList = needleMapper.findMESTestPCSRecentBYID(workIDList, time);

        //最近一周测试片数
        for (BIDNeedleEntity bidEntity : bidList) {
            String proberCardId = bidEntity.getProberCardId();
            if (!bidEntity.getWorkFlag().equals("是")) continue;
            for (Map<String, String> map : mesTestPCSList) {
                String id = map.get("ProberCardID");
                if (proberCardId.equals(id)) {
                    Object str_pcs = map.get("PCS");
                    //yosEntity.setPcs(Integer.parseInt(str_pcs.toString()));
                }
            }
        }

        //初始针长
        List<Map<String, String>> inComingLenList = needleMapper.getInComingLenByID(idList);
        for (BIDNeedleEntity bidEntity : bidList) {
            String proberCardId = bidEntity.getProberCardId();
            for (Map<String, String> map : inComingLenList) {
                String id = map.get("prober_card_id");
                if (proberCardId.equals(id)) {
                    String inComingLen = map.get("pin_maxlen");
                    bidEntity.setPinMinlen(Double.parseDouble(inComingLen));
                    break;
                }
            }
        }

        //磨耗比
        for (BIDNeedleEntity bidEntity : bidList) {

            Integer totalTd = bidEntity.getTotalTd();
            Double pinMinlen = bidEntity.getPinMinlen();
            Double afterPinlen = bidEntity.getAfterPinlen();
            pinMinlen = pinMinlen == null ? 0 : pinMinlen;
            afterPinlen = afterPinlen == null ? 0 : afterPinlen;
            if (pinMinlen <= afterPinlen) {
                bidEntity.setMohao(0);
                continue;
            }
            double mohao = totalTd / (pinMinlen - afterPinlen);
            bidEntity.setMohao((int) Math.round(mohao));
        }

        return bidList;
    }

    @Override
    public List<STHNeedleEntity> findSTHNeedleInfo(String time) {

        List<STHNeedleEntity> sthNeedleInfoList = needleMapper.findSTHNeedleInfoList();

        for (int i = sthNeedleInfoList.size() - 1; i >= 1; i--) {
            if (sthNeedleInfoList.get(i).getProberCardID()
                    .equals(sthNeedleInfoList.get(i - 1).getProberCardID())) {
                sthNeedleInfoList.remove(i);
            }
        }

        List<String> idList = sthNeedleInfoList.stream().map(aneEntity -> aneEntity.getProberCardID())
                .collect(Collectors.toList());


        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");

        for (STHNeedleEntity sthNeedleEntity : sthNeedleInfoList) {
            sthNeedleEntity.setUpdateTime(LocalDate.now().format(formatter));
        }

        //qty
        List<Map<String, String>> mesTestPCSRecentList = commonMapper.findMESTestPCSRecentBYID(idList);

        for (STHNeedleEntity sthEntity : sthNeedleInfoList) {
            String id = sthEntity.getProberCardID();
            for (Map<String, String> map : mesTestPCSRecentList) {
                if (map.get("ProberCardID").equals(id)) {
                    Integer pcs = Integer.parseInt(map.get("PCS") + "");
                    sthEntity.setQty(pcs);
                    break;
                }
            }
        }
        //
        List<Map<String, String>> oriLenList = needleMapper.getOriLenByID(idList);
        for (STHNeedleEntity sthEntity : sthNeedleInfoList) {
            for (Map<String, String> map : oriLenList) {
                if (sthEntity.getProberCardID().equals(map.get("proberCardId"))) {
                    Double oriLen = Double.parseDouble(map.get("pinMaxLen") + "");
                    sthEntity.setOriLen(oriLen);
                }
            }
        }


        //QTY、片数  --From Mes
        List<TDEntity> testRecord = this.getTestRecordMES(idList);

        //site数，复测系数  --From mysql
        List<TDEntity> testRecordCard = needleMapper.getTestRecordCard(idList);

        //将两个List，按照针卡ID，整合在一起
        List<TDEntity> paramList = testRecordCard.stream().flatMap(trc -> testRecord.stream().filter(tr -> tr.getProberCardID().equals(trc.getProberCardID())).map(tdEntity -> {
            tdEntity.setSite(trc.getSite());
            tdEntity.setCof(trc.getCof());
            return tdEntity;
        })).collect(Collectors.toList());

        for (STHNeedleEntity sthEntity : sthNeedleInfoList) {
            for (TDEntity tdEntity : paramList) {
                if (sthEntity.getProberCardID().equals(tdEntity.getProberCardID())) {
                    Integer totalTd = sthEntity.getTd();
                    double oriLen = sthEntity.getOriLen();
                    double len = sthEntity.getCurrentLen();
                    double lenSpec = sthEntity.getPinSpec();
                    if (len <= 6) lenSpec = 5;
                    if (len > 6) lenSpec = 6;
                    long qty = tdEntity.getDieQTY();
                    int pcs = tdEntity.getPCS();
                    int site = tdEntity.getSite();
                    double cof = tdEntity.getCof();
                    Double result = qty / pcs / site * cof;
                    int resu = result.intValue();
                    Integer p = Integer.valueOf(resu);

                    Double s = totalTd / (oriLen - len) * (len - lenSpec) / p;

                    if (oriLen == len) s = new Double(0);
                    sthEntity.setRemainCanTestPcs(Integer.valueOf(s.intValue()));

                    sthEntity.setTdPerPcs(p);
                    break;
                }
            }
        }


        List<Map<String, String>> noteList = needleMapper.getIQCNote(idList);

        //IQC PASS/维修返回IQC PASS/工程中/维修清针/内部归还→最近一次IQC的备注
        for (STHNeedleEntity sthEntity : sthNeedleInfoList) {
            String id = sthEntity.getProberCardID();
            //note
            for (Map<String, String> noteMap : noteList) {
                String proberId = noteMap.get("prober_card_id");
                String currentProcess = sthEntity.getCurrentProcess();
                if (id.equals(proberId) &&
                        (currentProcess.toUpperCase().equals("IQC_PASS")
                                || currentProcess.toUpperCase().equals("REIQC_PASS")
                                || currentProcess.toUpperCase().equals("IN_ENGINEERING")
                                || currentProcess.toUpperCase().equals("INNER_REPAIR")
                                || currentProcess.toUpperCase().equals("INNER_BACK")
                        )) {

                    String note = noteMap.get("note");
                    sthEntity.setNote(note);
                }
            }
        }


        for (STHNeedleEntity sthNeedleEntity : sthNeedleInfoList) {
            sthNeedleEntity.setPlanetCardFlag("Y");
            sthNeedleEntity.setOsat("Vtest");
            sthNeedleEntity.setHardType("Probe Card");
            sthNeedleEntity.setSc("PP06C100S7002A");


            String currentProcess = sthNeedleEntity.getCurrentProcess();

            if (currentProcess.toUpperCase().equals("CARD_IDLE")
                    || currentProcess.toUpperCase().equals("PRODUCTION_VERIFY")
                    || currentProcess.toUpperCase().equals("CARD_PM")
            ) {
                sthNeedleEntity.setStatus("可使用");
                //针卡待料/测试中/保养中/→空白
                sthNeedleEntity.setNote("");
            } else if (currentProcess.toUpperCase().equals("IQC_PASS")
                    || currentProcess.toUpperCase().equals("REIQC_PASS")
                    || currentProcess.toUpperCase().equals("IN_ENGINEERING")) {
                sthNeedleEntity.setStatus("待验证");
            } else if (currentProcess.toUpperCase().equals("UN_SEALED")) {
                sthNeedleEntity.setStatus("DOWN");
            } else if (currentProcess.toUpperCase().equals("OUT_FIXING")
                    || currentProcess.toUpperCase().equals("RE_BUILD")) {
                sthNeedleEntity.setStatus("寄针卡供应商");
            } else if (currentProcess.toUpperCase().equals("CUST_LENDING")) {
                sthNeedleEntity.setStatus("客户借出");
            }

            if (currentProcess.toUpperCase().equals("PRODUCTION_VERIFY")) {
                sthNeedleEntity.setSignPosition("领用");
            } else if (currentProcess.toUpperCase().equals("IN_ENGINEERING")) {
                sthNeedleEntity.setSignPosition("验证领用");
            } else if (currentProcess.toUpperCase().equals("CARD_IDLE") ||
                    currentProcess.toUpperCase().equals("IQC_PASS")
                    || currentProcess.toUpperCase().equals("REIQC_PASS")) {
                sthNeedleEntity.setSignPosition("闲置");
            } else if (currentProcess.toUpperCase().equals("OUT_FIXING")
                    || currentProcess.toUpperCase().equals("RE_BUILD")
                    || currentProcess.toUpperCase().equals("UN_SEALED")
                    || currentProcess.toUpperCase().equals("CUST_LENDING")) {
                sthNeedleEntity.setSignPosition("锁定");
            }


        }


        return sthNeedleInfoList;
    }

    @Override
    public List<ANENeedleEntity> findANENeedleInfo() {

        List<ANENeedleEntity> aneNeedleInfoList = needleMapper.findANENeedleInfoList();

        List<String> idList = aneNeedleInfoList.stream().map(aneEntity -> aneEntity.getId()).collect(Collectors.toList());


        for (ANENeedleEntity aneEntity : aneNeedleInfoList) {
            Double comingLen = aneEntity.getIncomingLen();
            Integer td = aneEntity.getTd();
            Double pinLen = aneEntity.getPinLen();
            Double lenSpec = aneEntity.getLenSpec();
            double v = td / (comingLen - pinLen) * (pinLen - lenSpec);
            //设置预估可测TD
            aneEntity.setPredictTD((int) v);
        }
        //qty
        List<Map<String, String>> mesTestPCSRecentList = commonMapper.findMESTestPCSRecentBYID(idList);

        for (ANENeedleEntity aneEntity : aneNeedleInfoList) {
            String id = aneEntity.getId();
            for (Map<String, String> map : mesTestPCSRecentList) {
                if (map.get("ProberCardID").equals(id)) {
                    Integer pcs = Integer.parseInt(map.get("PCS") + "");
                    aneEntity.setQty(pcs);
                    break;
                }
            }
        }

        //预计可测qty
        //QTY、片数  --From Mes
        List<TDEntity> testRecord = this.getTestRecordMES(idList);

        //site数，复测系数  --From mysql
        List<TDEntity> testRecordCard = needleMapper.getTestRecordCard(idList);

        //将两个List，按照针卡ID，整合在一起
        List<TDEntity> paramList = testRecordCard.stream().flatMap(trc -> testRecord.stream().filter(tr -> tr.getProberCardID().equals(trc.getProberCardID())).map(tdEntity -> {
            tdEntity.setSite(trc.getSite());
            tdEntity.setCof(trc.getCof());
            return tdEntity;
        })).collect(Collectors.toList());

        for (ANENeedleEntity aneEntity : aneNeedleInfoList) {
            for (TDEntity tdEntity : paramList) {
                if (aneEntity.getId().equals(tdEntity.getProberCardID())) {
                    Integer totalTd = aneEntity.getTd();
                    double oriLen = aneEntity.getIncomingLen();
                    double len = aneEntity.getPinLen();
                    double lenSpec = aneEntity.getLenSpec();
                    long qty = tdEntity.getDieQTY();
                    int pcs = tdEntity.getPCS();
                    int site = tdEntity.getSite();
                    double cof = tdEntity.getCof();
                    Double result = qty / pcs / site * cof;
                    int resu = result.intValue();
                    Integer p = Integer.valueOf(resu);

                    Double s = totalTd / (oriLen - len) * (len - lenSpec) / p;

                    if (oriLen == len) s = new Double(0);
                    aneEntity.setPredictQty(Integer.valueOf(s.intValue()));

                    //aneEntity.setTd(totalTd);
                    //zswEntity.setTestQty(pcs);
                    //zswEntity.setTdper(p);
                    break;
                }
            }
        }

        //已抽垫片数量
        // DD-垫片---2
        List<Map<String, String>> ddCountList = needleMapper.getDDCount(idList);

        for (ANENeedleEntity aneEntity : aneNeedleInfoList) {
            aneEntity.setCanDpCount(-1);
        }
        for (ANENeedleEntity aneEntity : aneNeedleInfoList) {
            String id = aneEntity.getId();
            for (Map<String, String> ddMap : ddCountList) {
                String ID = ddMap.get("id");
                String dd = ddMap.get("dd");
                boolean decreaseFlag = false;
                if (id.equals(ID) && dd.contains("抽垫片")) decreaseFlag = true;
                if (ID.equals("OM9098C1E1-PC-01") && id.equals("OM9098C1E1-PC-01")) {
                    String s = "";
                }
                setANEDDCount(aneEntity, decreaseFlag);
                if (decreaseFlag) aneEntity.setEdDpCount(aneEntity.getEdDpCount() + 1);
            }
        }
        for (ANENeedleEntity aneEntity : aneNeedleInfoList) {
            if (aneEntity.getCanDpCount() == -1) aneEntity.setCanDpCount(0);
        }

        return aneNeedleInfoList;
    }

    public List<TDEntity> getTestRecordMES(List<String> proberCardList) {
        List<TDEntity> testRecordMESList = commonMapper.getTestRecordMES(proberCardList);
        HashMap<String, TDEntity> map = new HashMap<>();
        for (TDEntity tdEntity : testRecordMESList) {
            String proberCardID = tdEntity.getProberCardID();
            if (map.get(proberCardID) == null) {
                TDEntity entity = new TDEntity();
                entity.setProberCardID(proberCardID);
                entity.setDieQTY(tdEntity.getDieQTY());
                entity.setPCS(tdEntity.getPCS());
                map.put(proberCardID, entity);
            } else {
                /*TDEntity entity = map.get(proberCardID);
                entity.setDieQTY(entity.getDieQTY() + tdEntity.getDieQTY());
                entity.setPCS(entity.getPCS() + tdEntity.getPCS());
                map.put(proberCardID, entity);*/
            }
        }

        ArrayList<TDEntity> tdEntitiesList = new ArrayList<>(map.values());
        return tdEntitiesList;
    }

    public void setANEDDCount(ANENeedleEntity aneEntity, boolean decreaseFlag) {
        String vendor = aneEntity.getVendor();

        int max_DDCount = 0;
        if (vendor.contains("强一")) {
            max_DDCount = 2;
        } else if (vendor.contains("中华精测")) {
            max_DDCount = 4;
        } else if (vendor.contains("TPI")) {
            max_DDCount = 3;
        } else if (vendor.contains("TSMC")) {
            max_DDCount = 3;
        }
        if (aneEntity.getCanDpCount() != -1) max_DDCount = aneEntity.getCanDpCount();
        aneEntity.setCanDpCount(max_DDCount);
        if (aneEntity.getCanDpCount() == 0) return;
        aneEntity.setCanDpCount(max_DDCount == 0 ? 0 : (decreaseFlag ? (--max_DDCount) : max_DDCount));

    }

    public static void main(String[] args) throws IOException {


        String s = "";
    }
}
